home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Database How-To / Visual Basic 4 Database - How-to (The Waite Group)(1995).iso / rptwrite.fr_ / rptwrite.fr
Text File  |  1995-07-20  |  13KB  |  484 lines

  1. VERSION 4.00
  2. Begin VB.Form frmReportWriter 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Visual Report Writer"
  5.    ClientHeight    =   5190
  6.    ClientLeft      =   1110
  7.    ClientTop       =   1530
  8.    ClientWidth     =   7320
  9.    Height          =   5685
  10.    Left            =   1005
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   5190
  13.    ScaleWidth      =   7320
  14.    Top             =   1140
  15.    Width           =   7530
  16.    Begin VB.PictureBox picHead 
  17.       Align           =   1  'Align Top
  18.       Appearance      =   0  'Flat
  19.       BackColor       =   &H00C0C0C0&
  20.       BorderStyle     =   0  'None
  21.       ForeColor       =   &H80000008&
  22.       Height          =   300
  23.       Left            =   0
  24.       ScaleHeight     =   300
  25.       ScaleWidth      =   7320
  26.       TabIndex        =   5
  27.       Top             =   0
  28.       Width           =   7320
  29.       Begin VB.CommandButton cmdSave 
  30.          Caption         =   "&Save File"
  31.          Height          =   252
  32.          Left            =   5160
  33.          TabIndex        =   2
  34.          Top             =   0
  35.          Width           =   972
  36.       End
  37.       Begin VB.CommandButton cmdQuit 
  38.          Caption         =   "&Quit"
  39.          Height          =   252
  40.          Left            =   6240
  41.          TabIndex        =   3
  42.          Top             =   0
  43.          Width           =   972
  44.       End
  45.       Begin VB.CommandButton cmdReport 
  46.          Caption         =   "&Create Report"
  47.          Default         =   -1  'True
  48.          Height          =   252
  49.          Left            =   120
  50.          TabIndex        =   0
  51.          Top             =   0
  52.          Width           =   1212
  53.       End
  54.       Begin VB.Label lblStatus 
  55.          Alignment       =   2  'Center
  56.          BorderStyle     =   1  'Fixed Single
  57.          Height          =   252
  58.          Left            =   1440
  59.          TabIndex        =   1
  60.          Top             =   24
  61.          Width           =   3612
  62.       End
  63.    End
  64.    Begin MSComDlg.CommonDialog cdBiblio 
  65.       Left            =   6240
  66.       Top             =   0
  67.       _Version        =   65536
  68.       _ExtentX        =   847
  69.       _ExtentY        =   847
  70.       _StockProps     =   0
  71.       CancelError     =   -1  'True
  72.       DefaultExt      =   "MDB"
  73.       DialogTitle     =   "BIBLIO.MDB Location"
  74.       FileName        =   "biblio.mdb"
  75.       Filter          =   "BIBLIO Database (biblio.mdb)|biblio.mdb|All Files (*.*)|*.*|"
  76.    End
  77.    Begin VB.OLE oleWord 
  78.       Height          =   4452
  79.       Left            =   120
  80.       OLETypeAllowed  =   1  'Embedded
  81.       TabIndex        =   4
  82.       Top             =   600
  83.       Width           =   7092
  84.    End
  85.    Begin VB.Menu mnuFratsaBlatz 
  86.       Caption         =   "&FratsaBlatz"
  87.       NegotiatePosition=   1  'Left
  88.       Visible         =   0   'False
  89.    End
  90. End
  91. Attribute VB_Name = "frmReportWriter"
  92. Attribute VB_Creatable = False
  93. Attribute VB_Exposed = False
  94. Option Explicit
  95.  
  96. Dim objWord As Object
  97. Dim pAppPath As String
  98.  
  99. Dim ColumnTabs(4) As String
  100. Dim ColumnHeaders(5) As String
  101. Dim ColumnWidths(5) As String
  102.  
  103. 'OLE Control Constants
  104. Const OLE_Activate As Integer = 7
  105. Const OLE_Deactivate As Integer = 9
  106.  
  107. Sub PrintColHeaders(Tabs() As String, ColHeaders() As String)
  108.     Dim i As Integer
  109.     
  110.     'Assumes cursor is at the beginning of the proper location
  111.     objWord.InsertPara
  112.     objWord.LineUp
  113.     objWord.FormatParagraph Before:="12 pt", _
  114.         After:="6 pt"
  115.     For i = 0 To UBound(Tabs)
  116.         objWord.FormatTabs Position:=Tabs(i) + Chr$(34), _
  117.             Align:=0
  118.     Next
  119.     For i = 0 To UBound(ColHeaders) - 1
  120.         objWord.INSERT ColHeaders(i) + Chr$(9)
  121.     Next
  122.     
  123.     With objWord
  124.         .StartOfLine
  125.         .SelectCurSentence
  126.         .CharRight 1, 1
  127.         .FormatFont Points:="12", _
  128.             Font:="Times New Roman", _
  129.             Bold:=1
  130.         .FormatBordersAndShading ApplyTo:=0, _
  131.             BottomBorder:=2
  132.         .LineDown
  133.     End With
  134. End Sub
  135.  
  136. Sub PrintFooter(Company As String)
  137.  
  138.     'Insert the report footer
  139.     objWord.ViewFooter
  140.     objWord.FormatTabs ClearAll:=1
  141.     objWord.FormatTabs Position:="7.0" + Chr$(34), _
  142.         DefTabs:="0.5" + Chr$(34), _
  143.         Align:=2, _
  144.         Leader:=0
  145.     objWord.StartOfLine
  146.     objWord.INSERT Company + Chr$(9) + "Page "
  147.     objWord.InsertPageField
  148.     objWord.SelectCurSentence
  149.     objWord.FormatFont Points:="12", _
  150.         Font:="Times New Roman", _
  151.         Bold:=1
  152.  
  153.     objWord.ViewFooter
  154. End Sub
  155.  
  156. Sub PrintReportTitle(Title As String)
  157.     With objWord
  158.         .StartOfDocument
  159.         .InsertPara
  160.         .StartOfDocument
  161.         .INSERT Title
  162.         .StartOfLine
  163.         .SelectCurSentence
  164.         .FormatFont Points:="18", _
  165.             Font:="Times New Roman", _
  166.             Bold:=1, _
  167.             Italic:=1
  168.         .CenterPara
  169.     
  170.         .FormatBordersAndShading ApplyTo:=0, _
  171.             Shadow:=0
  172.     
  173.         'Leave the cursor on the following line
  174.         .LineDown
  175.     End With
  176. End Sub
  177.  
  178. Sub SetColumnWidths()
  179.     Dim i As Integer
  180.     For i = LBound(ColumnTabs) To UBound(ColumnTabs)
  181.         If i Then
  182.             ColumnWidths(i) = Str$(Val(ColumnTabs(i)) - Val(ColumnTabs(i - 1)))
  183.         Else
  184.             ColumnWidths(i) = ColumnTabs(i)
  185.         End If
  186.     Next
  187. End Sub
  188.  
  189. Sub Status(txtCaption)
  190.     lblStatus.Caption = txtCaption
  191.     lblStatus.Refresh
  192. End Sub
  193.  
  194. Private Sub cmdQuit_Click()
  195.     Status "Ending application"
  196.     End
  197. End Sub
  198.  
  199. Private Sub cmdReport_Click()
  200.     Dim rptDB As DATABASE
  201.     Dim rptRS As Recordset
  202.     Dim Title As String
  203.     Dim i As Integer
  204.     Dim insertText As String
  205.     Dim strFileName As String
  206.     
  207.     Status "Opening database table"
  208.     
  209.     'If this is the first time running, put the Visual Basic
  210.     'path in the common dialog as the initial directory
  211.     If cdBiblio.InitDir = "" Then
  212.         cdBiblio.InitDir = "c:\vb"
  213.         cdBiblio.filename = cdBiblio.InitDir & "\" & cdBiblio.filename
  214.     End If
  215.     
  216.     cdBiblio.InitDir = App.Path
  217.     On Error GoTo userCanceled
  218.     Do While Dir(cdBiblio.filename) = ""
  219.         cdBiblio.ShowOpen
  220.     Loop
  221.     On Error GoTo 0
  222.  
  223.     Set rptDB = OpenDatabase(cdBiblio.filename)
  224.     Set rptRS = rptDB.OpenRecordset("All Titles")
  225.     
  226.     Status "Creating a new Word document"
  227.     
  228.     objWord.FileNew
  229.     Title = "Bibliography Database"
  230.     
  231.     Status "Inserting header and footer information"
  232.     PrintHeader Title, ColumnTabs(), ColumnHeaders()
  233.     PrintFooter "Enlighthened Software, Inc."
  234.     PrintReportTitle Title
  235.     PrintColHeaders ColumnTabs(), ColumnHeaders()
  236.     
  237.     'Start printing the report
  238.     Status "Adding data to report"
  239.     objWord.TableInsertTable NumColumns:=5, _
  240.         NumRows:=2, _
  241.         InitialColWidth:="2 in"
  242.     For i = 0 To 4
  243.         With objWord
  244.             .TableSelectColumn
  245.             .TableColumnWidth ColumnWidth:=ColumnWidths(i)
  246.             .NextCell
  247.             .NextCell
  248.         End With
  249.     Next
  250.     
  251.     'Format the paragraph height
  252.     objWord.TableSelectTable
  253.     objWord.FormatParagraph Before:="6 pt"
  254.     
  255.     'Select the first cell in the table
  256.     'objWord.TableSelectColumn
  257.     objWord.NextCell
  258.     
  259.     'On error resume is a handy way to ignore nulls
  260.     On Error Resume Next
  261.     Do While Not rptRS.EOF
  262.         With objWord
  263.             insertText = rptRS.Fields("Title")
  264.             .INSERT insertText
  265.             .NextCell
  266.             insertText = rptRS("ISBN")
  267.             .INSERT insertText
  268.             .NextCell
  269.             insertText = rptRS("Author")
  270.             .INSERT insertText
  271.             .NextCell
  272.             insertText = rptRS("Year Published")
  273.             .INSERT insertText
  274.             .NextCell
  275.             insertText = rptRS("Company Name")
  276.             .INSERT insertText
  277.             .NextCell
  278.             .TableInsertRow
  279.         End With
  280.         rptRS.MoveNext
  281.     Loop
  282.     On Error GoTo 0
  283.     
  284.     'Save the Word document
  285.     objWord.ToolsOptionsSave SummaryPrompt:=0
  286.     
  287.     strFileName = App.Path & "\TempRpt.doc"
  288.     'Word won't let us save a file over an existing document
  289.     If Len(Dir(strFileName)) Then
  290.         Kill strFileName
  291.     End If
  292.     objWord.FileSaveAs Name:=strFileName
  293.     
  294.     oleWord.CreateEmbed strFileName
  295.     oleWord.Refresh
  296.     
  297.     Status "Report complete"
  298.     
  299. OuttaHere:
  300.     Set rptDB = Nothing
  301.     Set rptRS = Nothing
  302.     Exit Sub
  303.     
  304. userCanceled:
  305.     Status "Report canceled by user"
  306.     Resume OuttaHere
  307.     
  308. End Sub
  309.  
  310.  
  311. Sub PrintHeader(Title As String, Tabs() As String, ColHeaders() As String)
  312.     Dim i As Integer
  313.  
  314.     With objWord
  315.         'For now, set DifferentFirstPage to no
  316.         .FilePageSetup TopMargin:="0.8" + Chr$(34), _
  317.             BottomMargin:="0.8" + Chr$(34), _
  318.             LeftMargin:="0.75" + Chr$(34), _
  319.             RightMargin:="0.75" + Chr$(34), _
  320.             ApplyPropsTo:=4, _
  321.             DifferentFirstPage:=0
  322.     End With
  323.                             
  324.     'Insert the report header
  325.     With objWord
  326.         .ViewHeader
  327.         .FormatTabs ClearAll:=1
  328.         .FormatTabs Position:="7.0" + Chr$(34), _
  329.             DefTabs:="0.5" + Chr$(34), _
  330.             Align:=2
  331.         .StartOfLine
  332.         .SelectCurSentence
  333.         .CharRight 1, 1
  334.         .FormatFont Points:="12", _
  335.             Font:="Times New Roman", _
  336.             Bold:=1
  337.         .StartOfLine
  338.         .INSERT Title + Chr$(9)
  339.         .InsertDateTime DateTimePic:="d' 'MMMM', 'yyyy", _
  340.             InsertAsField:=0
  341.         .InsertPara
  342.         .InsertPara
  343.     End With
  344.     
  345.     PrintColHeaders Tabs(), ColHeaders()
  346.     
  347.     objWord.ViewHeader   'Closes if it is open
  348.     
  349.     'Now set DifferentFirstPage
  350.     objWord.FilePageSetup DifferentFirstPage:=1
  351.     
  352. End Sub
  353.  
  354.  
  355.  
  356.  
  357. Private Sub cmdSave_Click()
  358.     WordFileSave oleWord
  359. End Sub
  360.  
  361.  
  362. Sub WordFileSave(OLECtrl As Control)
  363.     Dim WordObj As Object
  364.     Dim WBasic As Object
  365.     
  366.     'Activate the OLE control, and copy to the Clipboard
  367.     Status "Copying report to clipboard"
  368.     oleWord.Action = OLE_Activate
  369.     Set WBasic = CreateObject("Word.Basic")
  370.     WBasic.EditSelectAll
  371.     WBasic.EditCopy
  372.     oleWord.Action = OLE_Deactivate
  373.     
  374.     'Set up the properties for the FileSave common dialog
  375.     'and open to get the file save name
  376.     Status "Setting up file save"
  377.     cdBiblio.Filter = "Word Document (*.Doc)|*.doc"
  378.     cdBiblio.DefaultExt = "doc"
  379.     cdBiblio.filename = oleWord.SourceDoc
  380.     On Error GoTo FileSaveCancel:
  381.     cdBiblio.Action = 2
  382.     
  383.     'Check to see if the file exists - if it does, need new
  384.     'name for the file - Word can't overwrite an existing file
  385.     Do While Len(Dir$(cdBiblio.filename))
  386.         MsgBox "Please choose a new name for the file."
  387.         cdBiblio.Action = 2
  388.     Loop
  389.     On Error GoTo 0
  390.     
  391.     'Use a new instance of Word to save the document
  392.     Status "Saving Word document"
  393.     Set WBasic = Nothing
  394.     Set WordObj = GetObject("", "Word.Document.6")
  395.     Set WBasic = WordObj.Application.WordBasic
  396.     WBasic.FileNew
  397.     WBasic.EditPaste
  398.     WBasic.FileSaveAs cdBiblio.filename
  399.     WBasic.FileClose
  400.     
  401.     'Release the objects created in this procedure
  402.     Status "Report saved"
  403.     Set WBasic = Nothing
  404.     Set WordObj = Nothing
  405.     
  406. LeaveSub:
  407.     Exit Sub
  408.     
  409. FileSaveCancel:
  410.     Select Case Err.Number
  411.         Case 32755
  412.             'User pressed cancel
  413.             Status "Save canceled by user"
  414.             Resume LeaveSub
  415.         Case Else
  416.             Error Err.Number
  417.     End Select
  418. End Sub
  419.  
  420. Private Sub Form_Load()
  421.     Status "Creating a Word object"
  422.     Me.Show
  423.     Me.Refresh
  424.     
  425.     'Create a Microsoft Word object
  426.     Set objWord = GetObject("", "Word.Basic")
  427.     objWord.AppMinimize ("Microsoft Word")
  428.     
  429.     cmdReport.Enabled = True
  430.     cmdQuit.Enabled = True
  431.     
  432.     'Set up standard layout information
  433.     ColumnTabs(0) = "2.0"
  434.     ColumnTabs(1) = "3.25"
  435.     ColumnTabs(2) = "4.75"
  436.     ColumnTabs(3) = "5.25"
  437.     ColumnTabs(4) = "7.0"
  438.  
  439.     ColumnHeaders(0) = "Title"
  440.     ColumnHeaders(1) = "ISBN"
  441.     ColumnHeaders(2) = "Author"
  442.     ColumnHeaders(3) = "Year"
  443.     ColumnHeaders(4) = "Publisher"
  444.     
  445.     SetColumnWidths
  446.     
  447.     Status "Click on Create Report to create the report."
  448. End Sub
  449.  
  450.  
  451.  
  452. Private Sub Form_Resize()
  453.     Dim border As Integer
  454.     Dim winWidth As Integer
  455.     
  456.     border = picHead.Height
  457.     winWidth = Me.ScaleWidth
  458.     
  459.     cmdReport.Height = border
  460.     cmdQuit.Height = border
  461.     cmdSave.Height = border
  462.     
  463.     cmdQuit.Left = winWidth - cmdQuit.Width - cmdReport.Left
  464.     cmdSave.Left = cmdQuit.Left - cmdReport.Left - cmdSave.Width
  465.     lblStatus.Width = cmdSave.Left - lblStatus.Left - cmdReport.Left
  466.     lblStatus.TOP = (picHead.Height - lblStatus.Height) / 2
  467.     
  468.     oleWord.Left = border
  469.     oleWord.Width = Me.ScaleWidth - 2 * border
  470.     oleWord.Height = Me.ScaleHeight - oleWord.TOP - border
  471.     
  472. End Sub
  473.  
  474.  
  475. Private Sub Form_Unload(Cancel As Integer)
  476.     'Shut down Word
  477.     Set objWord = Nothing
  478. End Sub
  479.  
  480.  
  481.  
  482.  
  483.  
  484.